<template>
	<div class="content">
		<el-card class="box-card" shadow="hover">
			<div slot="header" class="clearfix">
				<span>系统设置</span>

			</div>
			<div class="list_box" style="float: left">
				<div class="list_box_col">
					<div class="list_box_col_tit">系统主货币</div>
					<el-select v-model="setting.mainCurrency" placeholder="请选择">
            <el-option v-for="item in currencyList"  :key="item.cid" :label="item.cnName" :value="item.cid">
              <span style="float: left">{{ item.cnName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.enName }}</span>
            </el-option>
					</el-select>
				</div>
				<div class="list_box_col">
					<div class="list_box_col_tit">默认营业税</div>
					<el-input style="width: 224px;" placeholder="请输入内容" v-model="setting.taxRate">
						<template slot="append">%</template>
					</el-input>
				</div>
				<div class="list_box_col">
					<div class="list_box_col_tit">换汇到账时间</div>
					<el-input style="width: 224px;" placeholder="请输入内容" v-model="setting.exchangeDay">
						<template slot="append">天</template>
					</el-input>
				</div>
        <div class="list_box_col">
          <el-button size="small" type="primary" @click="sub()">保存修改</el-button>
        </div>
			</div>
      <div style="width: 500px;height: 400px;float: right;margin-right: 350px;">
        <el-form ref="form" label-width="120px">
          <el-form-item label="最新公司Api" style="float: left" >
            <el-input v-model="CompanyApiKey" disabled style="width: 280px;" />
          </el-form-item>
          <el-button style="float: right" type="primary" @click="apiKeyUpdate">更新</el-button>
        </el-form>

      </div>
		</el-card>
	</div>
</template>

<script>
  import {listCurrency} from '@/api/system/currency';
  import {listBalance} from '@/api/system/balance';
  import {updateSetting,listSetting,getSetting,addSetting,} from '@/api/system/setting';
  import { getInfo } from "@/api/login";
  import { listCompany, getCompany, delCompany, addCompany, updateCompany, getApiKey} from "@/api/system/company";
	export default {
		data() {
			return {
        CompanyApiKey:null,
				inaccount: '台币 TWD',
        setting:{
          sid:null,
          mainCurrency:null,
          exchangeDay:null,
          taxRate:null,
        },
        currencyList:[],
        disabled:false
			}
		},
    created(){
		  this.getData();
		  getApiKey().then(data=>{
		    this.CompanyApiKey=data;
      })
    },
    methods:{
		  getData(){
        let that = this
        let aData = new Date();
        let date=aData.getFullYear() + "-" + (aData.getMonth() + 1) + "-" + aData.getDate()+" "+aData.getHours()+":"+aData.getMinutes()+":"+aData.getSeconds();
        getInfo().then(data=>{
          listSetting({ofCompany:data.tuser.ofCompany}).then(data1=>{
            if(data1.rows==''){
              let setting={}
              setting.mainCurrency=1;
              setting.taxRate='20';
              setting.modifytime=date;
              setting.exchangeDay='2';
              setting.ofCompany=data.tuser.ofCompany;
              addSetting(setting).then(rep=>{
                if(rep.code==200){
                  listSetting({ofCompany:data.tuser.ofCompany}).then(data2=> {
                    for(let itme in data2.rows){
                      this.setting.sid=data2.rows[itme].sid;
                      this.setting.mainCurrency=data2.rows[itme].mainCurrency;
                      this.setting.exchangeDay=data2.rows[itme].exchangeDay;
                      this.setting.taxRate=data2.rows[itme].taxRate;
                    }
                  })
                }
              })
            }else{
              for(let itme in data1.rows){
                this.setting.sid=data1.rows[itme].sid;
                this.setting.mainCurrency=data1.rows[itme].mainCurrency;
                this.setting.exchangeDay=data1.rows[itme].exchangeDay;
                this.setting.taxRate=data1.rows[itme].taxRate;
              }
            }

          })
        })

        listCurrency().then(date=>{
          this.currencyList=date.rows;
        })
      },
      sub(){
        updateSetting(this.setting).then(data=>{
          if(data.code==200){
            this.$message.success("修改成功！")
          }
        })
      },
      apiKeyUpdate(){
        getInfo().then(rep=>{
          updateCompany({id:rep.tuser.ofCompany,apiKey:this.CompanyApiKey}).then(data=>{
              if(data.code==200){
                this.$message.success("更新成功")
                this.getData();
                getApiKey().then(data=>{
                  this.CompanyApiKey=data;
                })
              }
          })
        })

      }
    }
	}
</script>

<style lang="scss">
	.list_box {
		&_col {

			&:first-child {
				padding-top: 0;
			}

			padding: 6px 0;
			display: flex;
			flex-direction: row;
			align-items: center;

			&_tit {
				margin-right: 8px;
			}
		}
	}

	.clearfix{
		width: 100%;
		height: 30px;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
</style>
